/*! ******************************************************************************
 *
 * Created on 2022年3月5日
 *
 * Copyright(c) YuZhou Big Data Laboratory CO.,Ltd, 2022.  All rights reserved.
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/
package org.yzbdl.lanius.ui.spoon.bottom;

import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.*;
import org.pentaho.di.ui.core.gui.LaniusGuiResource;
import org.pentaho.di.ui.core.gui.LaniusImage;

import com.google.common.collect.Maps;

/**
 * @author hs@yzbdl.ac.cn
 */
public class StateBar extends Composite {

    private CLabel stateText;

    private CoolBar coolBar;

    private StateButtonGroup tipsToolGroup;

    private Map<String, StateButtonGroup> stateButtonGroup = Maps.newHashMap();

    /**
     * @param parent
     * @param style
     */
    public StateBar(Composite parent) {
        this(parent, SWT.FILL);
    }

    /**
     * @param parent
     * @param style
     */
    private StateBar(Composite parent, int style) {
        super(parent, style);
        this.setLayout(new FormLayout());

        CoolBar coolBar = initStateCoolBar();
        Label separator = addLeftSeparator(coolBar);
        initStateText(separator);
    }

    private CoolBar initStateCoolBar() {
        coolBar = new CoolBar(this, SWT.FLAT | SWT.RIGHT_TO_LEFT);
        FormData coolBarData = new FormData();
        coolBarData.right = new FormAttachment(100, -3);
        coolBarData.top = new FormAttachment(0, 0);
        coolBarData.bottom = new FormAttachment(100, 0);
        coolBarData.width = 500;
        coolBar.setLayoutData(coolBarData);

//        initTipsToolBar();

        coolBar.setWrapIndices(null);
        resize(coolBar);
        coolBar.layout();

        coolBar.addMouseListener(new MouseListener() {
            @Override
            public void mouseUp(MouseEvent e) {
                coolBar.setWrapIndices(null);
                resize(coolBar);
                coolBar.layout();
            }

            @Override
            public void mouseDown(MouseEvent e) {}

            @Override
            public void mouseDoubleClick(MouseEvent e) {}
        });

        return coolBar;
    }

    private void initTipsToolBar() {
        tipsToolGroup = new StateButtonGroup(coolBar, "tipsTool");
        ToolItem toolItem = new ToolItem(tipsToolGroup.getToolBar(), SWT.NONE);
        toolItem.setToolTipText("小技巧");
        toolItem.setImage(LaniusGuiResource.getInstance().getCpImage(LaniusImage.TIPS, 10, 14));
    }

    /**
     * 修改CoolBar中CoolItem的尺寸
     * 
     * @param coolBar
     */
    private void resize(CoolBar coolBar) {
        CoolItem[] coolItems = coolBar.getItems();
        if (coolItems == null || coolItems.length == 0) {
            return;
        }
        for (CoolItem coolItem : coolItems) {
            Control control = coolItem.getControl();
            if (control == null) {
                continue;
            }
            Point controlSize = control.computeSize(SWT.DEFAULT, SWT.DEFAULT);
            if (control instanceof ToolBar) {
                ToolBar toolBar = (ToolBar)control;

                int y = controlSize.y;
                int x = controlSize.x;

                if (toolBar.getItemCount() > 0) {
                    coolItem.setMinimumSize(x, y);
                    coolItem.setPreferredSize(x, y);
                    coolItem.setSize(x, y);
                }
            }
        }
    }

    private Label addLeftSeparator(Control rightReference) {
        Label separator = new Label(this, SWT.SEPARATOR | SWT.VERTICAL | SWT.SHADOW_OUT);
        FormData separatorData = new FormData();
        separatorData.right = new FormAttachment(rightReference, -5);
        separatorData.top = new FormAttachment(0, 0);
        separatorData.bottom = new FormAttachment(100, 0);
        separatorData.width = 40;
        separator.setLayoutData(separatorData);
        return separator;
    }

    private void initStateText(Control rightReference) {
        stateText = new CLabel(this, SWT.SHADOW_NONE);
        // stateText.setText("渝州大数据实验室");
        FormData stateTextData = new FormData();
        stateTextData.right = new FormAttachment(rightReference, -5);
        stateTextData.left = new FormAttachment(0, 2);
        stateTextData.top = new FormAttachment(0, 0);
        stateTextData.bottom = new FormAttachment(100, 0);
        stateText.setLayoutData(stateTextData);
    }

    public StateButtonGroup addGroup(String name) {
        StateButtonGroup group = new StateButtonGroup(coolBar, name);
        stateButtonGroup.put(name, group);
        return group;
    }

    public StateButtonItem addGroupItem(String name, StateButtonItemAdapter adapter) {
        StateButtonGroup group = stateButtonGroup.get(name);
        if (group == null) {
            group = addGroup(name);
        }
        return group.addItem(adapter);
    }

    public void setStateText(CLabel stateText) {
        this.stateText = stateText;
    }

}
